package com.dhcc.android.base.db.Sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * 地块调查状态数据库管理类
 * 
 * @author zhoudan
 * @date 2016年1月26日
 */
public class DKSearchDBManager {

	private DBHelper helper;
	private SQLiteDatabase db; 

	private String table = "DKSearch";

	public DKSearchDBManager(Context context){
		helper = new DBHelper(context);
		db = helper.getWritableDatabase();

		db.execSQL("CREATE TABLE IF NOT EXISTS "+table + "  (dkid VARCHAR PRIMARY KEY  NOT NULL , f1 VARCHAR,f2 VARCHAR,f3 VARCHAR);");
	}

	//=====================固定的3个======================

	/**
	 * 保存编辑状态
	 * @param dkid  任务包名+调查期缩写 + DKBHU --如 "方正县QD234764"
	 * @param fkey  f1/f2/f3
	 * @param fkey  0--没有保存 ，1--保存了
	 * @return  三个便签页都有编辑  --true 反之--false
	 */
	public boolean changeStatus(String dkid,String fkey,String fvalue){
		System.out.println("--dkid--:"+dkid);
		db.beginTransaction();
		try{
			boolean flag = exits(dkid);
			if(flag){//修改
				ContentValues cv = new ContentValues();
				cv.put(fkey, fvalue);

				db.update(table, cv, "dkid = ?", new String[]{dkid});
			}else{//添加
				String sql = "insert into "+table + " values(?,?,?,?)";
				if(fkey.equals("f1")){
					db.execSQL(sql, new Object[]{dkid,fvalue,"0","0"});
				}else if(fkey.equals("f2")){
					db.execSQL(sql, new Object[]{dkid,"0",fvalue,"0"});
				}else if(fkey.equals("f3")){
					db.execSQL(sql, new Object[]{dkid,"0","0",fvalue});
				}
			}

			db.setTransactionSuccessful();
		}finally{
			db.endTransaction();
		}
		boolean f = complete(dkid);

		db.close();

		return f;
	}

	/**
	 * 判断对应的地块数据是否存在
	 */
	private boolean exits(String dkid){

		String sql = "select * from "+ table + " where dkid = '" + dkid +"'";
		Cursor c = db.rawQuery(sql, null);
		if(c.moveToFirst()){
			return true;
		}
		c.close();
		return false;
	}

	/**
	 * 是否全部有编辑
	 */
	private boolean complete(String dkid){
		int n = 0;
		String sql = "select * from "+ table + " where dkid = '" + dkid +"'";
		Cursor c = db.rawQuery(sql, null);
		if(c.moveToFirst()){
			n = c.getInt(c.getColumnIndex("f1"));
			n += c.getInt(c.getColumnIndex("f2"));
			n += c.getInt(c.getColumnIndex("f3"));
		}
		c.close();

		System.out.println("--n--:"+n);
		if(n == 3){
			return true;
		}

		return false;
	}


	//================动态的=================
	/**
	 * 保存编辑状态
	 * @param pagenum 便签页数
	 * @param dkid  任务包名+调查期缩写 + DKBHU --如 "方正县QD234764"
	 * @param fkey  f1/f2/f3
	 * @param fkey  0--没有保存 ，1--保存了
	 * @return  三个便签页都有编辑  --true 反之--false
	 */
	public boolean changeStatus(int pagenum,String dkid,String fkey,String fvalue){
		System.out.println("--dkid--:"+dkid);
		db.beginTransaction();
		try{
			boolean flag = exits(dkid);
			if(flag){//修改
				ContentValues cv = new ContentValues();
				cv.put(fkey, fvalue);

				db.update(table, cv, "dkid = ?", new String[]{dkid});
			}else{//添加
				String sql = "insert into "+table + " values(?,?,?,?)";
				if(fkey.equals("f1")){
					db.execSQL(sql, new Object[]{dkid,fvalue,"0","0"});
				}else if(fkey.equals("f2")){
					db.execSQL(sql, new Object[]{dkid,"0",fvalue,"0"});
				}else if(fkey.equals("f3")){
					db.execSQL(sql, new Object[]{dkid,"0","0",fvalue});
				}
			}

			db.setTransactionSuccessful();
		}finally{
			db.endTransaction();
		}
		boolean f = complete(dkid,pagenum);

		db.close();

		return f;
	}

	/**
	 * 是否全部有编辑
	 */
	private boolean complete(String dkid,int pagenum){
		int n = 0;
		String sql = "select * from "+ table + " where dkid = '" + dkid +"'";
		Cursor c = db.rawQuery(sql, null);
		if(c.moveToFirst()){
			n = c.getInt(c.getColumnIndex("f1"));
			n += c.getInt(c.getColumnIndex("f2"));
			n += c.getInt(c.getColumnIndex("f3"));
		}
		c.close();

		
		System.out.println("--n--:"+n);
		/*
		//必须所有标签都有调查结果，才标注为调查过
		if(n == pagenum || n > pagenum){
			return true;
		}
		*/
//		/*
		//只要有任意一个标签保存了，就标注为此条记录调查了
		if(n>0){
			return true;
		}
//		*/

		return false;
	}

}
